package com.kitty.game.fight.factory;

import com.kitty.game.role.model.Role;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.fight.bean.MonsterFightObject;
import com.kitty.game.boss.config.BossSet;
import com.kitty.game.config.PetSet;
import com.kitty.game.config.Skill;
import com.kitty.common.utils.SpringUtils;
import com.kitty.common.utils.PositionUtils;
import com.kitty.game.boss.service.NewBossService;
import com.kitty.game.fight.factory.model.NewComerFight;
import com.kitty.game.fight.message.vo.FightMember;
import com.kitty.game.map.service.MapService;
import com.kitty.game.school.service.SchoolService;
import com.kitty.game.task.model.product.entry.FightSide;
import com.kitty.game.task.model.product.entry.TaskFightEntry;
import com.kitty.game.task.model.product.targetparam.FightWinTargetParam;

import org.nutz.json.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class NewComerFightFactory extends FightFactory {

    @Override
    public NewComerFight create(int mapId, Object... params) {
        Role role = (Role) params[0];
        byte fightTargetType  = (Byte) params[1];
        int taskId  = (Integer) params[2];
        List<TaskFightEntry> fightTarget = (List<TaskFightEntry> ) params[3];
//        LoggerFunction.TASK.getLogger().info("{}({})任务开始创建战斗, 任务id: {}, 战斗目标类型：{}", role.getUid(), role.getName(), taskId, fightTargetType);
        FightSide enemyFightSide = getEnemyFightSide(role, fightTargetType, fightTarget);
        NewComerFight fight = new NewComerFight(role, enemyFightSide);
        fight.setTaskId(taskId);
        return fight;
    }


    /**
     * 获得用于生成战斗的敌方的战斗数据
     */
    private FightSide getEnemyFightSide(Role role, byte fightType, List<TaskFightEntry> fightEntries) {
        int monsterId = 2000000000;

        if (fightType == FightWinTargetParam.FIGHT_TO_PET) {
            FightSide fightSide = new FightSide();
            int index = 0;
            PetSet petSet = null;
            for (TaskFightEntry taskFightEntry : fightEntries) {
                petSet = SpringUtils.getPetService().getPetByName(taskFightEntry.getName());

                FightObject fightObject = new MonsterFightObject(petSet, PositionUtils.positions.get(index), monsterId);
                String name = (taskFightEntry.getNameInFight() != null ? taskFightEntry.getNameInFight() : petSet.getName());
                fightObject.setName(name);

                FightMember fightMember = fightObject.asFightMember();
                fightSide.getFightMemberList().add(fightMember);

                fightSide.getFightObjectList().add(fightObject);
                index++;
                monsterId++;
            }
            return fightSide;
        } else if (fightType == FightWinTargetParam.FIGHT_TO_MATCHSELF) {
            FightSide fightSide = new FightSide();
            int index = 0;
            for (TaskFightEntry taskFightEntry : fightEntries) {
                Logger logger = LoggerFactory.getLogger(getClass());
                logger.trace("taskFightEntry={}", Json.toJson(taskFightEntry));

                FightObject fightObject = new FightObject();
                fightObject.setPos(PositionUtils.positions.get(index));
                fightObject.setName(taskFightEntry.getName());
                /** 匹配玩家属性 */
                fightObject.setLevel(role.getLevel());
                fightObject.setId(monsterId);

                fightObject.setDefense(role.getDefense() / 2);
                fightObject.setSpeed((short) (role.getSpeed() / 2));
                fightObject.setMagPower(role.getMagPower() / 3);
                fightObject.setPhyPower(role.getPhyPower() / 3);
                fightObject.setMaxLife(role.getMaxLife() / 2);
                fightObject.setMonster(true);
                fightObject.setCurrLife(role.getMaxLife() * 2);
                if (taskFightEntry.getSkillIds() != null && taskFightEntry.getSkillIds().size() > 0) {
                    /** 只拿第1个技能的相性 */
                    int skillId = taskFightEntry.getSkillIds().get(0);
                    Skill skill = SpringUtils.getSkillService().getSkillInfo(skillId);
                    fightObject.setPolar((short) skill.getSkillJob());
                    fightObject.setSkillIds(new ArrayList<>(taskFightEntry.getSkillIds()));
                }

                fightObject.setIcon(taskFightEntry.getIcon());
                fightSide.getFightObjectList().add(fightObject);

                FightMember fightMember = new FightMember();
                fightMember.setId(monsterId);
                fightMember.setWeapon_icon((short) 0);
                fightMember.setPosition(PositionUtils.positions.get(index));
                fightMember.setIcon(taskFightEntry.getIcon());
                fightMember.setList(fightObject.asFieldValues());
                fightSide.getFightMemberList().add(fightMember);

                index++;
                monsterId++;
            }
            return fightSide;
        } else if (fightType == FightWinTargetParam.FIGHT_TO_OTHER_SCHOOL) {
            FightSide fightSide = new FightSide();

            SchoolService schoolService = SpringUtils.getBean(SchoolService.class);
            int index = 0;
            for (TaskFightEntry taskFightEntry : fightEntries) {
                Logger logger = LoggerFactory.getLogger(getClass());
                logger.trace("taskFightEntry={}", Json.toJson(taskFightEntry));

                FightObject fightObject = new FightObject();
                fightObject.setPos(PositionUtils.positions.get(index));
                fightObject.setName(schoolService.replaceSchoolDesc((byte) role.getPolar(), taskFightEntry.getName()));

                /** 匹配玩家属性 */
                fightObject.setMonster(true);
                fightObject.setLevel(role.getLevel());
                fightObject.setId(monsterId);

                fightObject.setDefense(role.getDefense());
                fightObject.setSpeed((role.getSpeed()));
                fightObject.setMagPower(role.getMagPower());
                fightObject.setPhyPower(role.getPhyPower());
                fightObject.setMaxLife(role.getMaxLife());
                fightObject.setCurrLife(role.getMaxLife());
                byte toFightSchool = schoolService.getToFightSchool((byte) role.getPolar());
                List<Integer> skillIds = schoolService.getToFightTaskSkillIds((byte) role.getPolar());
                if (skillIds != null && skillIds.size() > 0) {
                    fightObject.setPolar(toFightSchool);
                    fightObject.setSkillIds(new ArrayList<>(skillIds));
                }

                byte toFightSex = schoolService.getToFightTaskSex(toFightSchool);
                short icon = SpringUtils.getBean(MapService.class).heroIcon(toFightSex, toFightSchool);
                fightObject.setIcon(icon);
                short weaponIcon = schoolService.getSchoolWeaponIcon(toFightSchool);
                fightObject.setWeaponId(weaponIcon);
                fightSide.getFightObjectList().add(fightObject);

                FightMember fightMember = new FightMember();
                fightMember.setId(monsterId);
                fightMember.setWeapon_icon(weaponIcon);
                fightMember.setPosition(PositionUtils.positions.get(index));
                fightMember.setIcon(taskFightEntry.getIcon());
                fightMember.setList(fightObject.asFieldValues());
                fightSide.getFightMemberList().add(fightMember);

                index++;
                monsterId++;
            }

            return fightSide;
        } else if (fightType == FightWinTargetParam.FIGHT_TO_BOSS || fightType == FightWinTargetParam.PROTECT_FLYING_PET) {
            FightSide fightSide = new FightSide();

            NewBossService bossService = SpringUtils.getBean(NewBossService.class);

            int index = 0;
            BossSet bossSet = null;
            for (TaskFightEntry taskFightEntry : fightEntries) {
                bossSet = bossService.getBossSet(taskFightEntry.getName());
                short position = PositionUtils.positions.get(index);

                FightObject fightObject = new FightObject();
                fightObject.setPos(position);
                String name = (taskFightEntry.getNameInFight() != null ? taskFightEntry.getNameInFight() : bossSet.getName());
                fightObject.setName(name);
                fightObject.setLevel(role.getLevel());
                fightObject.setId(monsterId);

                fightObject.setDefense(bossSet.getFangyu() * role.getLevel());
                fightObject.setSpeed( (bossSet.getSudu() * role.getLevel()));
                fightObject.setMagPower(bossSet.getFagong() * role.getLevel());
                fightObject.setPhyPower(bossSet.getWugong() * role.getLevel());
                fightObject.setMaxLife(bossSet.getQixue() * role.getLevel());
                fightObject.setCurrLife(bossSet.getQixue() * role.getLevel());
                fightObject.setMonster(true);
                fightObject.setPolar((short) bossSet.getXiangxin());
                fightObject.setSkillIds(bossSet.getSkills());
                fightObject.setIcon(bossSet.getIcon());
                fightObject.setBossSet(bossSet);
                fightSide.getFightObjectList().add(fightObject);

                FightMember fightMember = new FightMember();
                fightMember.setId(monsterId);
                fightMember.setWeapon_icon((short) 0);
                fightMember.setPosition(position);
                fightMember.setIcon((short) bossSet.getIcon());
                fightMember.setList(fightObject.asFieldValues());
                fightSide.getFightMemberList().add(fightMember);

                index++;
                monsterId++;
            }


            return fightSide;
        }
        return null;
    }

}
